﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visa.DB;
using System.Data;

namespace Visa.Service
{
    public class ThongtinhuuService
    {
        private VisaEntities db = new VisaEntities();

        public int addOrUpdateVisa(Thongtinhuuich entity)
        {
            db.Entry(entity).State = entity.Id == -1 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
            return entity.Id;
        }

        public void DeleteThongtinhuich(int id)
        {
            var c = db.Thongtinhuuiches.SingleOrDefault(l => l.Id == id);
            c.Detele = true;
            db.SaveChanges();
        }

        public Thongtinhuuich GetThongtinhuichById(int id)
        {
            try
            {
                return db.Thongtinhuuiches.SingleOrDefault(l => l.Id == id);
            }
            catch { return null; }
        }

        public List<Thongtinhuuich> GetAllThongtinhuich()
        {
            try
            {
                return db.Thongtinhuuiches.Where(o => o.Detele == false).OrderByDescending( o  => o.DateCreate).ToList();
            }
            catch { return null; }
        }

        public List<Thongtinhuuich> GetAllThongtinhuichByQuocGiaID(int id)
        {
            try
            {
                return db.Thongtinhuuiches.Where(o => o.Detele == false && (o.QuocGia == id || id == -1)).OrderByDescending( o => o.DateCreate).ToList();
            }
            catch { return null; }
        }

        public List<Thongtinhuuich> GetListTTHIByStringAndId(string reqcde, int id)
        {
            try
            {
                return db.Thongtinhuuiches.Where(o => o.Detele == false && o.Regioncode == reqcde && (o.QuocGia == id || id == -1)).OrderByDescending(o => o.DateCreate).ToList();
            }
            catch { return null; }
        }

        public List<Thongtinhuuich> GetListTTHiALl(int id)
        {
            try
            {
                return db.Thongtinhuuiches.Where(o => o.Detele == false && (o.QuocGia == id || id == -1)).OrderByDescending(o => o.DateCreate).ToList();
            }
            catch { return null; }
        }

    }
}
